package com.handyshop.mall.basic.redis.util;

import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author hs
 * @date 2020/4/23 13:47
 */
@Repository(value = "RedisUtil")
@RequiredArgsConstructor
public class RedisUtil {
    private final StringRedisTemplate stringRedisTemplate;
    private final RedisTemplate redisTemplate;

    /**
     * 存
     *
     * @param key   key
     * @param value 值
     */
    public void set(String key, Object value) {
        redisTemplate.boundValueOps(key).set(value);
    }

    /**
     * 存
     *
     * @param key        key
     * @param value      值
     * @param expireTime 过期时间
     */
    public void set(String key, Object value, Long expireTime) {
        redisTemplate.boundValueOps(key).set(value, expireTime, TimeUnit.SECONDS);
    }

    public Object get(String key) {
        return redisTemplate.boundValueOps(key).get();
    }

    /**
     * redis自增键
     *
     * @param key          key
     * @param value        初始值
     * @param initialValue 过期时间(秒)
     */
    public void setIncr(String key, int value, long initialValue) {
        RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        counter.set(value);
        counter.expire(initialValue, TimeUnit.SECONDS);
    }

    public long getIncr(String key, long expire) {
        RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        counter.expire(expire, TimeUnit.SECONDS);
        return counter.incrementAndGet();
    }

    public void addMap(String key, String field, Object value) {
        redisTemplate.boundHashOps(key).put(field, value);
    }

    public void addMap(String key, String field, Object value, long time) {
        redisTemplate.boundHashOps(key).put(field, value);
        redisTemplate.boundHashOps(key).expire(time, TimeUnit.SECONDS);
    }

    public void setMapValue(String key, String mapKey, Object mapValue) {
        BoundHashOperations<String, String, Object> boundHashOperations = redisTemplate.boundHashOps(key);
        boundHashOperations.put(mapKey, mapValue);
    }

    public void deleteMapValue(String key, String mapKey) {
        BoundHashOperations<String, String, Object> boundHashOperations = redisTemplate.boundHashOps(key);
        boundHashOperations.delete(mapKey);
    }

    public void remove(String key) {
        redisTemplate.delete(key);
    }

    public boolean exists(String key) {
        return redisTemplate.hasKey(key);
    }

    public <T> T getMapField(String key, String field) {
        return (T) redisTemplate.boundHashOps(key).get(field);
    }

    public Map<String, Object> getMap(String key) {
        return redisTemplate.boundHashOps(key).entries();
    }

    public void removeMapField(String key, Object... field) {
        redisTemplate.boundHashOps(key).delete(field);
    }

}
